<?php

namespace app\common\service;

use app\common\model\banxi\sys\SysConfig;
use think\facade\Cache;

class ConfigService extends ApiService
{

    /**
     * 获取系统配置
     * @param $name string 配置名称
     * @return false|null|string
     */
    public function getSysConfigValue(string $name): false|null|string
    {
        $key = 'sys_config_' . $name;
        $value = Cache::get($key);
        if ($value) {
            return $value;
        }
        $configValue = SysConfig::where('configKey', $name)->value('configValue');
        if ($configValue) {
            Cache::tag('sys_config')->set($key, $configValue, 60 * 5);
        }
        return $configValue;
    }

    public function getSysConfigValueList($name)
    {
        $key = 'sys_config_list_' . $name;
        $value = Cache::get($key);
        if ($value) {
            return $value;
        }
        $names = explode(',', $name);
        $configs = SysConfig::whereIn('configKey', $names)->select();
        $result = [];
        foreach ($configs as $item) {
            $result[$item['configKey']] = $item['configValue'];
        }
        if ($result) {
            Cache::tag('sys_config')->set($key, $result, 60 * 5);
        }
        return $result;
    }

}